<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class stock_mod extends module
{
    function __construct()
    {
        parent::__construct();
    }
    function index()
    {
    }
    function store_list()
    {
        $all = $this->db->get_all("select * from " . table('stock_store') . " order by id asc");
        if ($all) {
            foreach ($all as $k => $v) {
                $data[$v['id']] = $v['title'];
            }
            return $data;
        }
    }
    function bill_info($id)
    {
        if ($id) {
            $row = $this->db->get_one("select * from " . table("stock_bill") . " where id = " . $id);
            $receive_row = $this->db->get_one("select * from " . table('order_address') . " where bid = " . $row['id']);
            if ($receive_row['kd_id']) {
                $kd_row = $this->module('express')->get_config($receive_row['kd_id']);
                $receive_row['kd_code'] = $kd_row['code'];
                $receive_row['kd_name'] = $kd_row['name'];
                $receive_row['hide_mobile'] = hideMobile($receive_row['mobile']);
            }
            $row['receive'] = $receive_row;
            $send_row = $this->db->get_one("select * from " . table('stock_store') . " where id = " . $row['store_id']);
            $plist = $this->db->get_all("select * from " . table('stock_bill_pro') . " where bid = " . $row['id']);
            if ($plist) {
                $unit_type = $this->module('stock')->unit_type();
                foreach ($plist as $k => $v) {
                    $v['unit'] = $unit_type[$v['unit']];
                    $parr[] = $v;
                }
                $row['product'] = $parr;
            }
            $row['addtime'] = _date($row['addtime']);
            $row['send'] = $send_row;
            return $row;
        } else {
            return false;
        }
    }
    function order_info($order_no)
    {
        $unit_type = $this->module('stock')->unit_type();
        $orow = $this->db->get_one("select * from " . table("order_info") . " where order_no = '" . $order_no . "'");
        $order_list = $this->db->get_all("select * from " . table('order_goods') . " where oid = " . $orow['id']);
        if ($order_list) {
            $barcode_in = '';
            foreach ($order_list as $k => $v) {
                $barcode_in .= "'" . $v['barcode'] . "',";
                $buy_arr[$v['barcode']] = $v['buy_num'];
            }
            $barcode_in = rtrim($barcode_in, ',');
            $stock_list = $this->db->get_all("select * from " . table('stock_product') . " where barcode in(" . $barcode_in . ")");
            if ($stock_list) {
                $total_pay_price = 0;
                foreach ($stock_list as $k => $v) {
                    $v['pid'] = $v['id'];
                    $v['num'] = $buy_arr[$v['barcode']];
                    $v['price'] = $v['price_sale'];
                    $shop_price = $v['num'] * $v['price'];
                    $v['total_price'] = $shop_price;
                    $v['unit'] = $unit_type[$v['unit']];
                    $parr[] = $v;
                    $total_pay_price += $shop_price;
                }
            }
            $row['product'] = $parr;
        }
        $row['promote_price'] = '0.00';
        $row['other_price'] = '0.00';
        $row['total_price'] = $total_pay_price;
        $row['pay_price'] = $total_pay_price;
        $row['oid'] = $orow['id'];
        $receive_row = $this->db->get_one("select * from " . table('order_address') . " where oid = " . $orow['id']);
        $row['receive'] = $receive_row;
        return $row;
    }
    function unit_type()
    {
        $all = $this->db->get_all("select * from " . table('stock_unit') . " order by px asc");
        if ($all) {
            foreach ($all as $k => $v) {
                $data[$v['id']] = $v['name'];
            }
            return $data;
        }
    }
    function seller_type()
    {
        return array('0' => '零售价格', '1' => '批发价格', '2' => '最低售价', '3' => '进货价格');
    }
}